package com.qian.apiserver.controller;

import com.qian.apiserver.model.Count;
import com.qian.apiserver.model.MatchRecord;
import com.qian.apiserver.model.MatchRecordModel;
import com.qian.apiserver.service.IMatchRecordService;
import com.yaowk.api.common.PageReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/game/userSchedule")
@EnableScheduling
@Api(tags = "用户统计")
public class UserScheduleController {
    @Autowired
    private IMatchRecordService matchRecordService;
    //写方法根据时间查询比赛记录数据
    //并统计时间查询后的比赛记录的投注量和获取筹码量放入数据库
    //把当前时间-2小时到当前时间传入方法中


//   // 每2小时执行一次
//    @Scheduled(cron = "0 */1 *  * * * " )
//    public void cron(){
//        MatchRecordModel model = new MatchRecordModel();
//        model.setBeginDate(new Date(new Date().getTime()+6*60*60*1000));
//        model.setEndDate(new Date(new Date().getTime()+8*60*60*1000));
//        PageReq pageReq = new PageReq();
//        all(pageReq,model);
//        System.out.print("统计每隔2个小时统计的数据");
//    }
    // 每天执行一次
    @Scheduled(cron = "00 00 00 * * ? " )
    public void cronDay(){
        MatchRecordModel model = new MatchRecordModel();
        model.setCode((byte) 0);
        long current = System.currentTimeMillis();

        long zero = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        model.setBeginDate(new Date(zero-24*60*60*1000));
        model.setEndDate(new Date(zero));
        PageReq pageReq = new PageReq();
        all(pageReq,model);
        System.out.print("统计每天统计的数据");
    }

     //每周周一执行一次
    @Scheduled(cron = "0 0 0 ? * MON")
    public void cronWeek(){
        MatchRecordModel model = new MatchRecordModel();
        model.setCode((byte) 1);
        model.setBeginDate(new Date(new Date().getTime()-7*8*60*60*1000));
        model.setEndDate(new Date(new Date().getTime()));
        PageReq pageReq = new PageReq();
        List list = all(pageReq,model);

        System.out.print("统计每周的数据");
    }
    //每月执行一次
    @Scheduled(cron = "0 0 0 1 * ?")
    public void cronMonth(){
        MatchRecordModel model = new MatchRecordModel();
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//设置当前日期
        calendar.add(Calendar.MONTH, -1);//月份减一
        System.out.println(calendar.getTime());//输出上个月的日期
        model.setCode((byte) 2);
        model.setBeginDate(new Date(new Date().getTime()-7*8*60*60*1000));
        model.setEndDate(new Date(new Date().getTime()));
        PageReq pageReq = new PageReq();
        List list = all(pageReq,model);

        System.out.print("统计每月的数据");
    }

    //每天中每分钟的数据
    @GetMapping("/count")
    @ApiOperation("每分钟的数据")
    public Map allMinute(PageReq pageReq,MatchRecordModel model){
        List<Count> list = new ArrayList<Count>();
        long current = System.currentTimeMillis();
        long zero = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        for(long i = zero;i<new Date().getTime();i+=60*1000){
         model.setBeginDate(new Date(new Date(i).getTime()));
         model.setEndDate(new Date(new Date(i).getTime()+60*1000));
         Count count = matchRecordService.allMinute(pageReq,model);
         list.add(count);
        }
        Map map = new LinkedHashMap();
        map.put("list",list);
        return map;
    }

    @GetMapping("/countNumber")
    @ApiOperation("总用户下注量和筹码量")
    private MatchRecord countNumber(MatchRecord matchRecord){
        List<MatchRecord> matchRecords = matchRecordService.findAll();
        Double bettingAmount = 0.00;
        Double winChipAmount = 0.00;
        for(MatchRecord matchRecord1:matchRecords){
            bettingAmount +=  matchRecord1.getBetting().doubleValue();
            winChipAmount += matchRecord1.getWinChip().doubleValue();
        }
        matchRecord.setBetting(new BigDecimal(bettingAmount));
        matchRecord.setWinChip(new BigDecimal(winChipAmount));
        return matchRecord;
    }

    @GetMapping("/all")
    @ApiOperation("查询")
    public List all(PageReq pageReq, MatchRecordModel model){
        return matchRecordService.findSechedule(pageReq,model);
    }

}
